using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.Utils;
using TMS.Domain.Entity;
using Newtonsoft.Json.Linq;
namespace TMS.Api.Controllers
{

    public class CourseList : ControllerBase
    {
        //         var CourseList = new CourseList(
        //  _IdentityService,
        //  _Class,
        //  _Students,
        //  _Term,
        //  _Course,
        //  _Subject
        // );
        // //所有学期所有班级的课程的科目
        // var CourseLists = CourseList.Courses().ToList();



        //班级
        private readonly IRepository<Class> _Class;
        private readonly IIdentityService _IdentityService;

        private readonly IRepository<Students> _Students;

        //学期
        private readonly IRepository<Term> _Term;

        //课程表
        private readonly IRepository<Course> _Course;
        //科目表
        private readonly IRepository<Subject> _Subject;


        public CourseList(
            IIdentityService IdentityService,
            IRepository<Class> Class,
            IRepository<Students> Students,
            IRepository<Term> Term,
            IRepository<Course> Course,
            IRepository<Subject> Subject

        )
        {
            _Subject = Subject;
            _Course = Course;
            _Term = Term;
            _Class = Class;
            _Students = Students;
            _IdentityService = IdentityService;

        }


        public class CourseLists
        {
            /// <summary>
            ///  课程id
            /// </summary>
            /// <value></value>

            public Guid CourseId { get; set; }
            /// <summary>
            /// 班级Id
            /// </summary>
            /// <value></value>
            public Guid ClassId { get; set; }

            /// <summary>
            /// 学期ID
            /// </summary>
            /// <value></value>
            public Guid TermId { get; set; }

            /// <summary>
            /// 科目ID
            /// </summary>
            /// <value></value>
            public Guid? SubjectId { get; set; }

            /// <summary>
            /// 科目名称
            /// </summary>
            /// <value></value>
            public string SubjectName { get; set; }

            /// <summary>
            ///  教师id
            /// </summary>
            /// <value></value>
            public string TeacherId { get; set; }

            /// <summary>
            ///  课程id
            /// </summary>
            /// <value></value>
            // public string TeacherName { get; set; }


        }

        public List<CourseLists> Courses()
        {
            List<CourseLists> CourseLists = new List<CourseLists>();
            //课程表
            var CourseList = _Course.Table.ToList();
            //班级列表
            var ClassList = _Class.Table.ToList();
            //所有科目表
            var SubjectList = _Subject.Table.ToList();

            foreach (var item in CourseList)
            {
                var Decide = ClassList.Where(x => x.Id == item.ClassId).FirstOrDefault();

                if (Decide == null)
                {
                    _Course.Delete(item.Id);
                }
                //字符串转对象
                var CourseText = JObject.Parse(item.CourseText);
                // "[[{id:50},{id:50}],[{id:50},{},{}]]"
                //字符串转数组
                //  var arr1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(item.CourseText);
                // string arr0_= ((string?)items["id"]);
                //      var arr0 = JObject.Parse("{}");
                // string arr0_0 = ((string?)arr0["id"]);
                foreach (var items in CourseText)
                {
                    foreach (var t in items.Value)
                    {
                        try
                        {


                            //科目名称
                            var SubjectName = ((string?)t.First["SubjectName"]);
                            //地点
                            var Place = ((string?)t.First["Place"]);
                            //科目id
                            var SubjectId = ((Guid?)t.First["SubjectId"]);
                            //课程id
                            var id = item.Id;
                            // 班级id
                            var ClassId = item.ClassId;
                            //学期id
                            var TermId = item.TermId;
                            // 科目信息
                            var Subject = SubjectList.Where(x => x.Id == SubjectId).FirstOrDefault();
                            if (Subject != null)
                            {
                                // 教师Id 
                                var TeacherId = Subject.TeacherId;
                                //添加到CourseLists
                                CourseLists.Add(new CourseLists
                                {
                                    ClassId = ClassId,
                                    TermId = TermId,
                                    SubjectId = SubjectId,
                                    SubjectName = SubjectName,
                                    TeacherId = TeacherId,
                                    CourseId = id
                                });
                            }
                        }
                        catch (System.Exception)
                        {

                            //SubjectId错误为了不影响使用进入这里

                        }

                    }


                }




            }

            return CourseLists.ToList();
        }
        /// <summary>
        /// 查找某学期所有班的课程
        /// </summary>
        /// <param name="termId">学期id</param>
        /// <returns></returns>
        public List<CourseLists> Courses(Guid termId)
        {
            List<CourseLists> CourseLists = new List<CourseLists>();
            //课程表
            var CourseList = _Course.Table.Where(x => x.TermId == termId).ToList();
            //班级列表
            var ClassList = _Class.Table.ToList();
            //所有科目表
            var SubjectList = _Subject.Table.ToList();

            foreach (var item in CourseList)
            {
                var Decide = ClassList.Where(x => x.Id == item.ClassId).FirstOrDefault();

                if (Decide == null)
                {
                    _Course.Delete(item.Id);
                }
                //字符串转对象
                var CourseText = JObject.Parse(item.CourseText);
                // "[[{id:50},{id:50}],[{id:50},{},{}]]"
                //字符串转数组
                //  var arr1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(item.CourseText);
                // string arr0_= ((string?)items["id"]);
                //      var arr0 = JObject.Parse("{}");
                // string arr0_0 = ((string?)arr0["id"]);
                foreach (var items in CourseText)
                {
                    foreach (var t in items.Value)
                    {
                        try
                        {


                            //科目名称
                            var SubjectName = ((string?)t.First["SubjectName"]);
                            //地点
                            var Place = ((string?)t.First["Place"]);
                            //科目id
                            var SubjectId = ((Guid?)t.First["SubjectId"]);
                            //课程id
                            var id = item.Id;
                            // 班级id
                            var ClassId = item.ClassId;
                            //学期id
                            var TermId = item.TermId;
                            // 科目信息
                            var Subject = SubjectList.Where(x => x.Id == SubjectId).FirstOrDefault();
                            if (Subject != null)
                            {
                                // 教师Id 
                                var TeacherId = Subject.TeacherId;
                                //添加到CourseLists
                                CourseLists.Add(new CourseLists
                                {
                                    ClassId = ClassId,
                                    TermId = TermId,
                                    SubjectId = SubjectId,
                                    SubjectName = SubjectName,
                                    TeacherId = TeacherId,
                                    CourseId = id
                                });
                            }
                        }
                        catch (System.Exception)
                        {

                            //SubjectId错误为了不影响使用进入这里

                        }

                    }


                }




            }

            return CourseLists.ToList();
        }
        /// <summary>
        /// 查找某学期某个班的课程
        /// </summary>
        /// <param name="termId">学期id</param>
        /// <param name="classId">班级id</param>
        /// <returns></returns>
        public List<CourseLists> Courses(Guid termId, Guid classId)
        {
            List<CourseLists> CourseLists = new List<CourseLists>();
            //课程表
            var CourseList = _Course.Table.Where(x => x.TermId == termId && x.ClassId == classId).ToList();
            //班级列表
            var ClassList = _Class.Table.ToList();
            //所有科目表
            var SubjectList = _Subject.Table.ToList();

            foreach (var item in CourseList)
            {
                var Decide = ClassList.Where(x => x.Id == item.ClassId).FirstOrDefault();

                if (Decide == null)
                {
                    _Course.Delete(item.Id);
                }
                //字符串转对象
                var CourseText = JObject.Parse(item.CourseText);
                // "[[{id:50},{id:50}],[{id:50},{},{}]]"
                //字符串转数组
                //  var arr1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(item.CourseText);
                // string arr0_= ((string?)items["id"]);
                //      var arr0 = JObject.Parse("{}");
                // string arr0_0 = ((string?)arr0["id"]);
                foreach (var items in CourseText)
                {
                    foreach (var t in items.Value)
                    {
                        try
                        {


                            //科目名称
                            var SubjectName = ((string?)t.First["SubjectName"]);
                            //地点
                            var Place = ((string?)t.First["Place"]);
                            //科目id
                            var SubjectId = ((Guid?)t.First["SubjectId"]);
                            //课程id
                            var id = item.Id;
                            // 班级id
                            var ClassId = item.ClassId;
                            //学期id
                            var TermId = item.TermId;
                            // 科目信息
                            var Subject = SubjectList.Where(x => x.Id == SubjectId).FirstOrDefault();
                            if (Subject != null)
                            {
                                // 教师Id 
                                var TeacherId = Subject.TeacherId;
                                //添加到CourseLists
                                CourseLists.Add(new CourseLists
                                {
                                    ClassId = ClassId,
                                    TermId = TermId,
                                    SubjectId = SubjectId,
                                    SubjectName = SubjectName,
                                    TeacherId = TeacherId,
                                    CourseId = id
                                });
                            }
                        }
                        catch (System.Exception)
                        {

                            //SubjectId错误为了不影响使用进入这里

                        }

                    }


                }




            }

            return CourseLists.ToList();
        }

        /// <summary>
        /// 查找班级课表
        /// </summary>
        /// <param name="termId">学期id</param>
        /// <param name="classId">班级id</param>
        /// <returns></returns>
        public List<CourseLists> Courses_Class(Guid classId)
        {
            List<CourseLists> CourseLists = new List<CourseLists>();
            //课程表
            var CourseList = _Course.Table.Where(x =>x.ClassId == classId).ToList();
            //班级列表
            var ClassList = _Class.Table.ToList();
            //所有科目表
            var SubjectList = _Subject.Table.ToList();

            foreach (var item in CourseList)
            {
                var Decide = ClassList.Where(x => x.Id == item.ClassId).FirstOrDefault();

                if (Decide == null)
                {
                    _Course.Delete(item.Id);
                }
                //字符串转对象
                var CourseText = JObject.Parse(item.CourseText);
                // "[[{id:50},{id:50}],[{id:50},{},{}]]"
                //字符串转数组
                //  var arr1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(item.CourseText);
                // string arr0_= ((string?)items["id"]);
                //      var arr0 = JObject.Parse("{}");
                // string arr0_0 = ((string?)arr0["id"]);
                foreach (var items in CourseText)
                {
                    foreach (var t in items.Value)
                    {
                        try
                        {


                            //科目名称
                            var SubjectName = ((string?)t.First["SubjectName"]);
                            //地点
                            var Place = ((string?)t.First["Place"]);
                            //科目id
                            var SubjectId = ((Guid?)t.First["SubjectId"]);
                            //课程id
                            var id = item.Id;
                            // 班级id
                            var ClassId = item.ClassId;
                            //学期id
                            var TermId = item.TermId;
                            // 科目信息
                            var Subject = SubjectList.Where(x => x.Id == SubjectId).FirstOrDefault();
                            if (Subject != null)
                            {
                                // 教师Id 
                                var TeacherId = Subject.TeacherId;
                                //添加到CourseLists
                                CourseLists.Add(new CourseLists
                                {
                                    ClassId = ClassId,
                                    TermId = TermId,
                                    SubjectId = SubjectId,
                                    SubjectName = SubjectName,
                                    TeacherId = TeacherId,
                                    CourseId = id
                                });
                            }
                        }
                        catch (System.Exception)
                        {

                            //SubjectId错误为了不影响使用进入这里

                        }

                    }


                }




            }

            return CourseLists.ToList();
        }
    }
}